package com.tm.purchase.controller;

import com.tm.purchase.domain.Product;
import com.tm.purchase.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Date 2025-3-5
 * @Author: Cjj
 */
@RestController
@RequestMapping("/Product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/getAllProducts")
    public List<Product> getAllProducts(Product product) {
        List<Product> products = productService.getAllProducts(product);
        return products;
    }

    @GetMapping("/{id}")
    public List<Product> getProductById(@PathVariable Long id) {
          List<Product> ById =   productService.getProductById(id);
          return ById;

    }

    @PostMapping("/saveProduct")
    public List<Product> saveProduct(@ModelAttribute Product product) {
        product.setName(product.getName());
        product.setDescription(product.getDescription());
        product.setPrice(product.getPrice());
        product.setQuantity(product.getQuantity());
        return  productService.saveProduct(product);
    }

    @GetMapping("/edit/{id}")
    public List<Product> showEditForm(@PathVariable Long id, Product product) {
        return  productService.editProduct(id, product);
    }


    @GetMapping("/delete/{id}")
    public String deleteProduct(@PathVariable Long id) {

        return  productService.deleteProduct(id);
    }



}
